Dr D’s COVID Cave

Creating Some Faceted Maps

Setting Everything Up

#load covid data from the UK Government Website
COVID_MSOA <- read_csv("https://coronavirus.data.gov.uk/downloads/msoa_data/MSOAs_latest.csv")
## 
## -- Column specification -------------------------------------------------------
## cols(
##   .default = col_double(),
##   rgn19_cd = col_character(),
##   rgn19_nm = col_character(),
##   utla19_cd = col_character(),
##   utla19_nm = col_character(),
##   lad19_cd = col_character(),
##   lad19_nm = col_character(),
##   msoa11_cd = col_character(),
##   msoa11_hclnm = col_character()
## )
## i Use `spec()` for the full column specifications.
#*Data Cleaning Task
#*
#*Think about how we could useful re-code -99 
#*which currently codes cases between 0-2
#*it might be that a new re-coded column is created in the
#*long tidy version of the data

#get some boundaries
#super-generalised url to geojson
url <- "https://opendata.arcgis.com/datasets/87aa4eb6393644768a5f85929cc704c2_0.geojson"

#read those boundaries into an sf() object for plotting
UK_MSOA <- geojson_sf(url)

Joining boundaries and data together

#join the MSOA boundaries to the data
MSOA_COVID_Map <- UK_MSOA %>% 
  merge(.,
        COVID_MSOA,
        by.x="MSOA11CD",
        by.y="msoa11_cd")

First Test Map

## tmap mode set to interactive viewing
## Variable(s) "wk_41" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.

An alternative with ggplot2

#here's a quick and dirty map with a continuous scale
#using ggplot2
ggplot(data = MSOA_COVID_Map,aes(fill=wk_41)) +
  geom_sf(color='transparent') +
  scale_fill_viridis_c(option = 'plasma')

Creating Some Faceted Maps

Check the data looks OK first

types <- sapply(MSOA_COVID_Map, class)

The data we want to plot starts from column 9 onwards therefore, pivot from column 9 onwards:

MSOA_COVID_Long <- COVID_MSOA %>% 
  pivot_longer(.,
               c(9:46))

Now re-merge the boundary geometries to the long file:

MSOA_COVID_Map_Long <- UK_MSOA %>% 
  merge(.,
        MSOA_COVID_Long,
        by.x="MSOA11CD",
        by.y="msoa11_cd")

Now plot a faceted map using ggplot facet map

ggplot(data = MSOA_COVID_Map_Long,aes(fill=value)) +
  geom_sf(color='transparent') +
  scale_fill_viridis_c(option = 'plasma')+
  facet_wrap(~name)

We could try ranging the data like Rich Harris did:

range(MSOA_COVID_Map_Long$value)
## [1] -99 683
#create a new rank column
MSOA_COVID_Map_Long$value_rank <- rank(MSOA_COVID_Map_Long$value)

Now plot a ranked faceted map using ggplot facet map and lets go for 4 rows this time like Rich did…

ggplot(data = MSOA_COVID_Map_Long,aes(fill=value_rank)) +
  geom_sf(color='transparent') +
  scale_fill_viridis_c(option = 'plasma')+
  facet_wrap(~name, nrow = 4)